package com.schooleducation.controller.teacher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.schooleducation.entity.Lesson;
import com.schooleducation.entity.LessonContent;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;
import com.schooleducation.uitls.DateJsonValueProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("/teacher/lessonInfo")
public class TeacherLessonInfoController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;

	/**
	 * 获取页面
	 * 
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String getPage(HttpSession session, Model model) {
		User teacher = (User) session.getAttribute("teacher");
		if (teacher != null) {
			List<Lesson> list = serviceFac.getLessonService().getLessonListByTeacher(teacher);
			model.addAttribute("teacher", teacher);
			model.addAttribute("lessonList", list);
			if(list.size() > 0) {
				model.addAttribute("contentList", serviceFac.getLessonService().getLessonContentList(list.get(0).getId()));
			}

			return "teacher/lessonInfo";
		} else {
			return "redirect:../login";
		}
	}

	/**
	 * 根据课程获取内容列表
	 * 
	 * @param lessonId
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getContentListByLesson(int lessonId) {
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "contentInfo", "lesson" });
		config.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
		return JSONArray.fromObject(serviceFac.getLessonService().getLessonContentList(lessonId), config).toString();
	}

	/**
	 * 获取课题的详细信息页面
	 * 
	 * @param contentId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "show/{id:\\+?[1-9][0-9]*$}", method = RequestMethod.GET)
	public String getContentInfo(@PathVariable(value = "id") int contentId, HttpSession session, Model model) {
		User teacher = (User) session.getAttribute("teacher");
		if (teacher != null) {
			//课程课题基本内容
			LessonContent lc = serviceFac.getLessonService().getLessonContentById(contentId);
			//教师基本信息
			model.addAttribute("teacher", teacher);
			//课程基本信息
			model.addAttribute("lesson", serviceFac.getLessonService().getLessonById(lc.getLesson().getId()));
			model.addAttribute("content", lc);
			//课程课题详细内容
			model.addAttribute("contentInfo", serviceFac.getLessonService().getLessonContentInfo(contentId));
			
			return "teacher/showContentInfo";
		} else {
			return "redirect:../../../login";
		}
	}

	/**
	 * 更新课题页面
	 * 
	 * @param contentId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "update/{id:\\+?[1-9][0-9]*$}", method = RequestMethod.GET)
	public String updateContentInfo(@PathVariable(value = "id") int contentId, HttpSession session, Model model) {
		User teacher = (User) session.getAttribute("teacher");
		if (teacher != null) {
			//课程课题基本内容
			LessonContent lc = serviceFac.getLessonService().getLessonContentById(contentId);
			//教师基本信息
			model.addAttribute("teacher", teacher);
			//课程基本信息
			model.addAttribute("lesson", serviceFac.getLessonService().getLessonById(lc.getLesson().getId()));
			model.addAttribute("content", lc);
			//课程课题详细内容
			model.addAttribute("contentInfo", serviceFac.getLessonService().getLessonContentInfo(contentId));

			return "teacher/updateContentInfo";
		} else {
			return "redirect:../../../login";
		}
	}

	/**
	 * 更新课题内容
	 * 
	 * @param session
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "updateContent", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateContent(HttpSession session, String data) {
		User teacher = (User) session.getAttribute("teacher");
		Map<String, Object> state = new HashMap<String, Object>();
		if (teacher == null) {
			state.put("isLogin", "no");
		} else {
			state.put("isLogin", "yes");
			LessonContent lc = (LessonContent) JSONObject.toBean(JSONObject.fromObject(data), LessonContent.class);
			if (serviceFac.getLessonService().updateLessonContent(lc)) {
				state.put("update", "success");
				state.put("page", "../../lessonInfo");
			} else {
				state.put("update", "fail");
			}
		}

		return JSONObject.fromObject(state).toString();
	}

	/**
	 * 删除课题
	 * @param session
	 * @param contentId
	 * @return
	 */
	@RequestMapping(value = "deleteContent", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String deleteContent(HttpSession session, int contentId) {
		User teacher = (User) session.getAttribute("teacher");
		Map<String, Object> state = new HashMap<String, Object>();
		if (teacher == null) {
			state.put("isLogin", "no");
		} else {
			state.put("isLogin", "yes");
			if (serviceFac.getLessonService().deleteLessonContent(contentId)) {
				state.put("del", "success");
			} else {
				state.put("del", "fail");
			}
		}

		return JSONObject.fromObject(state).toString();
	}
}
